package com.shwread.android.qysw.step;

import android.util.Log;

import com.shwread.android.qysw.step.dto.RedPackageDto;
import com.shwread.android.qysw.step.dto.UserStepDTO;
import com.shwread.android.qysw.tools.DataKeeperHelper;
import com.shwread.android.qysw.tools.TimeHelper;


/**
 * Created by Fantasy on 16/3/20.
 */
public class UserStepModel {
    private static final String TAG_USER_STEP = "tag_user_step";
    private static final String TAG_RED_PACKAGE = "tag_red_package";
    private UserStepDTO mUserStepDTO;
    private RedPackageDto mRedPackageDto;

    private static class UserStepModelHolder {
        public final static UserStepModel sington = new UserStepModel();
    }

    public static UserStepModel getInstance() {
        return UserStepModelHolder.sington;
    }

    private UserStepModel() {
        tryLoadLocal();
    }

    private boolean tryLoadLocal() {
        mUserStepDTO = (UserStepDTO) DataKeeperHelper.getInstance().getUserStepCache().get(TAG_USER_STEP);
        return mUserStepDTO != null;
    }

    public int getUserTodayStep() {
        int stepNum = 0;
        if (tryLoadLocal()) {
            if (TimeHelper.getInstance().getTodayString().equals(mUserStepDTO.getDate())) {//是今天
                stepNum = mUserStepDTO.getStepNums();
            }
        }
        return stepNum;
    }

    public double getUserTodayCalories() {
        double calories = 0.0f;
        if (tryLoadLocal()) {
            if (TimeHelper.getInstance().getTodayString().equals(mUserStepDTO.getDate())) {//是今天
                calories = mUserStepDTO.getCalories();
            }
        }
        return calories;
    }

    public double getUserTodayDistance() {
        double distance = 0.0f;
        if (tryLoadLocal()) {
            if (TimeHelper.getInstance().getTodayString().equals(mUserStepDTO.getDate())) {//是今天
                distance = mUserStepDTO.getDistance();
            }
        }
        return distance;
    }
//
//    public void updateTodayStep(int stepNum) {
//        Log.i("darren", "updateStep:" + stepNum);
//        UserStepDTO userStepDTO = new UserStepDTO();
//        userStepDTO.setDate(TimeHelper.getInstance().getTodayString());
//        userStepDTO.setStepNums(stepNum);
//        DataKeeperHelper.getInstance().getUserStepCache().put(TAG_USER_STEP, userStepDTO);
//    }
//
//    public void updateTodayCalories(double calories) {
//        Log.i("darren", "updateTodayCalories:" + calories);
//        UserStepDTO userStepDTO = new UserStepDTO();
//        userStepDTO.setDate(TimeHelper.getInstance().getTodayString());
//        userStepDTO.setCalories(calories);
//        DataKeeperHelper.getInstance().getUserStepCache().put(TAG_USER_STEP, userStepDTO);
//    }
//
//    public void updateTodayDistance(double distance) {
//        Log.i("darren", "updateTodaydistance:" + distance);
//        UserStepDTO userStepDTO = new UserStepDTO();
//        userStepDTO.setDate(TimeHelper.getInstance().getTodayString());
//        userStepDTO.setDistance(distance);
//        DataKeeperHelper.getInstance().getUserStepCache().put(TAG_USER_STEP, userStepDTO);
//    }

    public void updateTodayHealthyData(int stepNum, double calories, double distance) {
        Log.i("darren", "updateStep:" + stepNum);
        UserStepDTO userStepDTO = new UserStepDTO();
        userStepDTO.setDate(TimeHelper.getInstance().getTodayString());
        userStepDTO.setStepNums(stepNum);
        userStepDTO.setCalories(calories);
        userStepDTO.setDistance(distance);
        DataKeeperHelper.getInstance().getUserStepCache().put(TAG_USER_STEP, userStepDTO);
    }

    public boolean isHasGetRedPackage() {
        boolean res = false;
        mRedPackageDto = (RedPackageDto) DataKeeperHelper.getInstance().getUserStepCache().get(TAG_RED_PACKAGE);
        if (mRedPackageDto != null) {
            res = mRedPackageDto.isHasGetPackage();
        }
        return res;
    }

    public void updateRedPackage() {
        RedPackageDto redPackageDto = new RedPackageDto();
        redPackageDto.setDate(TimeHelper.getInstance().getTodayString());
        redPackageDto.setHasGetPackage(true);
        DataKeeperHelper.getInstance().getUserStepCache().put(TAG_RED_PACKAGE, redPackageDto);
    }


}
